package cityOperations;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;

import dataEntity.City;

/**
 * Session Bean implementation class CitySessionBean
 */
@Stateless
@LocalBean
public class CitySessionBean {
	
	//@PersistenceContext(unitName="CitySessionBean")
	@PersistenceContext(unitName = "CT_EJB", type = PersistenceContextType.EXTENDED)
	private EntityManager em;

//	@PersistenceUnit(unitName="CT_EJB")
//	private EntityManagerFactory factory;

	/**
     * Default constructor. 
     */
    public CitySessionBean() {
    	
    }
    
    public List<City> getCities_MoreThan(int threshold){
    	
    	List<City> retList = new ArrayList<City>();
//    	factory = Persistence.createEntityManagerFactory("CT_EJB");
//    	em = factory.createEntityManager();
    	
    	//Query query = em.createQuery("SELECT * FROM city");

    	
    	Query query = em.createNativeQuery("SELECT * FROM city", City.class);
    	Collection<City> selectResult = (Collection<City>) query.getResultList();
    	
    	for(City c : selectResult){
    		if(c.getPopulation() > threshold){
    			retList.add(c);
    		}
    	}
 
    	
    	return retList;
    }
    
    public List<?> getCities_LessThan(int threshold){
    	List<Object> retList = new ArrayList<Object>();
    	
    	return retList;
    }

}
